<?php
/**
 * Created by PhpStorm.
 * User: mabao
 * 消息：系统消息和聊天消息和聊天
 */
namespace app\api\controller;
use think\Db;
use think\Config;
class Build extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            echo json_encode(['status'=>301,'msg'=>"Token失效！请重新登录！"]);
//            exit;
//        }
    }
    /**
     * 更多楼盘
     *  shi：当前定位的市id
     */
    public function moreBuildData(){
        extract(input());
        $where = array();
        $where['b_status'] = 1;
        //1、区域筛选：shi、qu、street都要传
        if($qu){
            $where['b_qu'] = $qu;
        }else{
            $where['b_shi'] = $shi;
        }
        if($street){
            $where['b_street'] = $street;
        }
        //2、面积筛选：不限都为空，第一档的最小传0，最后一档的最大传1000000000
//        if($price_min && $price_max){
        if($price_max){
//            if(!$price_min){ $price_min = 0; }
//            if($price_min == 500 || $price_min == 5000000){
//                $where['b_price'] = array('>=',$price_min);
//            }else{
//                $where['b_price'] = array('between',"$price_min,$price_max");
//            }
            $where['b_price|b_price_max'] = array('between',"$price_min,$price_max");
        }
        //3、面积筛选：不限都为空，第一档的最小传1，最后一档的最大传1000000
        if($mianji_min && $mianji_max){
            $where['b_mianji_min|b_mianji_max'] = array('between',"$mianji_min,$mianji_max");
        }

        $data = Db::table('up_building')
            ->where($where)
            ->field('b_id,b_title,b_title_bottom,b_img,b_mianji_min,b_mianji_max,b_unit_price,b_is_sale,b_huxing,b_hot_title,b_address')
            ->order('b_tuijiang desc,b_time desc')
            ->page ($page,10)
            ->select ();
        return json(['status'=>200,'data'=>$data]);
    }
    /**
     * 楼盘详情
     *  shi：当前定位的市id
     */
    public function buildInfo(){
        extract(input());
        $where = array();
        $where['b_id'] = $b_id;
        $data = Db::table('up_building')
            ->where($where)
            ->find ();
        if(!$data || $data['b_status'] == 2){
            return json(['status'=>100,'data'=>'此房源不存在或已下架！']);
        }
        //轮播图
        if($data['b_images']){
            $data['b_images'] = explode(',',$data['b_images']);
        }
        //标签
        if($data['b_sign']){
            $data['b_sign'] =  Db::table('up_build_signs')
                ->where('bs_id','in',$data['b_sign'])
                ->page(1,5)
                ->column('bs_name');
        }
        //贷款
        $dknl = Db::table('up_about')->where('id',4)->value('content');
        $mww_dk =  new \app\api\controller\Show();
        $res = $mww_dk->debx(360,($data['b_price'] - $data['b_shoufu'])*10000,$dknl);
        $data['b_yuegong'] = $res['data'][0]['month'];
        //户型图和推荐
        $huxings = Db::table('up_district_hu')
            ->where('dh_type',2)
            ->where('dh_dis_id',$b_id)
            ->select();
        $builds = Db::table('up_building')
            ->where('b_status',1)
            ->order('b_tuijiang desc,b_updatetime desc')
            ->field('b_id,b_title,b_title_bottom,b_img,b_mianji_min,b_mianji_min,b_unit_price,b_is_sale,b_huxing,b_hot_title,b_address')
            ->page(1,5)
            ->select();
        return json(['status'=>200,'msg'=>$data,'huxings'=>$huxings,'builds'=>$builds]);
    }
    /**
     * 楼盘详情获取动态或者顾问
     *  shi：当前定位的市id
     */
    public function buildInfoOther(){
        extract(input());
        $where = array();
        $where['b_id'] = $b_id;
        $data = Db::table('up_building')
            ->where($where)
            ->field('b_user_ids,b_status')
            ->find ();
        if(!$data || $data['b_status'] == 2){
            return json(['status'=>100,'data'=>'此房源不存在或已下架！']);
        }
        //置业顾问和动态
        if($type == 1){
            $data = Db::table('up_u_user')
                ->where('user_type',2)
                ->where('user_status',1)
                ->where('user_id','in',$data['b_user_ids'])
                ->page(1,5)
                ->field('user_id,user_username,user_shop,user_image,user_phone')
                ->select();
        }else{
            $data = Db::table('up_build_news')
                ->where('bn_b_id',$b_id)
                ->order('bn_timed desc')
                ->page(1,5)
                ->field('bn_content',true)
                ->select();
        }
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 收集卖房信息
     * @return \think\response\Json
     */
    public function formHouse()
    {
        extract(input());
        $data = array();
        $data['city'] = $city;
        $data['xiaoqu'] = $xiaoqu;
        $data['house'] = $house;
        $data['money'] = $money;
        $data['phone'] = $phone;
        $data['name'] = $name;
        $data['sex'] = $sex;
        $data['time'] = date('Y-m-d H:i:s');
        $data['uid'] = $user_id;
        $res = Db::table('up_form')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>0,'msg'=>'操作失败']);
        }
    }
    /**
     * 收集装修信息
     * @return \think\response\Json
     */
    public function formFit()
    {
        extract(input());
        $data = array();
        $data['city'] = $city;
        $data['mianji'] = $mianji;
        $data['shi'] = $shi;
        $data['wei'] = $wei;
        $data['ting'] = $ting;
        $data['yusuan'] = $yusuan;
        $data['phone'] = $phone;
        $data['name'] = $name;
        $data['sex'] = $sex;
        $data['time'] = date('Y-m-d H:i:s');
        $data['uid'] = $user_id;
        $res = Db::table('up_form_fit')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>0,'msg'=>'操作失败']);
        }
    }

}